package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzx.model.User;
import com.lzx.model.jzny.Jzny;
import com.lzx.model.wlwjd.Land;
import com.lzx.model.zjwd.Zjwd;
import com.lzx.service.UserService;
import com.lzx.service.jzny.JznyService;
import com.lzx.service.wlwjd.LandService;
import com.lzx.utils.ConnectionUtil;
import com.lzx.utils.MethodUtil;
import com.lzx.utils.SessionUtil;
import com.sun.org.apache.bcel.internal.generic.LAND;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sy/jzny")
public class JznyController {
    @Autowired
    JznyService jznyService;
    @Autowired
    UserService userService;
    @Autowired
    LandService landService;


    @RequestMapping("/jzny")
    public ModelAndView jzny(HttpServletRequest request) throws SQLException {
        String landid = request.getParameter("dkid");
        //System.out.println("lanid------"+lanid);

        ModelAndView mv = new ModelAndView("system/jzny");
        Land land = landService.findLandName(landid);
        String userkey = land.getUserkey();
        String pagenum = "1";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //查询最后一条数据
        String lasturl = "http://47.106.191.129/shuju/nydsjjk/listone.html?userkey=" + userkey;
        String lastdoPostString = ConnectionUtil.DoPostString(lasturl, null);
        JSONObject lastobj = JSON.parseObject(lastdoPostString);

        Jzny lastjzny = new Jzny();
        lastjzny.setMassifId(land.getMassifId());
        lastjzny.setWindDirection(lastobj.getString("fx"));
        lastjzny.setWindSpeed(lastobj.getString("fs"));
        lastjzny.setRainfall(lastobj.getString("yl"));
        lastjzny.setAirTemperature(lastobj.getString("kqwd"));
        lastjzny.setAirHumidity(lastobj.getString("kqsd"));
        lastjzny.setSoilTemperature(lastobj.getString("trwd"));
        lastjzny.setSoilMoisture(lastobj.getString("trsd"));
        lastjzny.setLightIntensity(lastobj.getString("gz"));
        lastjzny.setcO2concentration(lastobj.getString("eyht"));
        //String createTime=lastobj.getString("createTime");
        // lastjzny.setTime(createTime.substring(0,4)+createTime.substring(5,7)+createTime.substring(8,10) +createTime.substring(11,13)+createTime.substring(14,16)+createTime.substring(17,19)+"00");
        try {
            lastjzny.setCreatetime(sdf.parse(lastobj.getString("createTime")));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        mv.addObject("jznyLast", lastjzny);

        String url = "http://47.106.191.129/shuju/nydsjjk/listDo.html?userkey=" + userkey + "&pagenum=" + pagenum;

        String doPostString = ConnectionUtil.DoPostString(url, null);
        JSONObject obj = JSON.parseObject(doPostString);
        String titleNum = obj.getString("total");
        mv.addObject("titleNum", titleNum);

        JSONArray jArray = obj.getJSONArray("rows");
        List<Jzny> jznyAllPage = new ArrayList<>();
        if (jArray.size() != 0) {
            for (int i = 0; i < jArray.size(); i++) {
                JSONObject jsonObject = jArray.getJSONObject(i);
                Jzny jz = new Jzny();
                jz.setMassifId(jsonObject.getString("id"));
                jz.setWindDirection(jsonObject.getString("fx"));
                jz.setWindSpeed(jsonObject.getString("fs"));
                jz.setRainfall(jsonObject.getString("yl"));
                jz.setAirTemperature(jsonObject.getString("kqwd"));
                jz.setAirHumidity(jsonObject.getString("kqsd"));
                jz.setSoilTemperature(jsonObject.getString("trwd"));
                jz.setSoilMoisture(jsonObject.getString("trsd"));
                jz.setLightIntensity(jsonObject.getString("gz"));
                jz.setcO2concentration(jsonObject.getString("eyht"));
                String createTime1 = lastobj.getString("createTime");
                jz.setTime(createTime1.substring(0, 4) + createTime1.substring(5, 7) + createTime1.substring(8, 10) + createTime1.substring(11, 13) + createTime1.substring(14, 16) + createTime1.substring(17, 19) + "00");

                try {
                    jz.setCreatetime(sdf.parse(jsonObject.getString("createTime")));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                jznyAllPage.add(jz);

            }
        }
        mv.addObject("jznyAllPage", jznyAllPage);
        mv.addObject("landid", landid);

        if (SessionUtil.getUser(request).getIsAdmin().equals("Y")) {
            mv.addObject("isAdmn", "TRUE");
        } else {
            mv.addObject("isAdmn", "FALSE");
        }

        return mv;
    }

    @RequestMapping("/jznylist")
    public Object jznylist(HttpServletRequest request) throws SQLException {
        String landid = request.getParameter("landid");
        String pagenum = "1";
        Land land = landService.findLandName(landid);
        String userkey = land.getUserkey();

        String url = "http://47.106.191.129/shuju/nydsjjk/listDo.html?userkey=" + userkey + "&pagenum=" + pagenum;

        String doPostString = ConnectionUtil.DoPostString(url, null);
        JSONObject obj = JSON.parseObject(doPostString);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        JSONArray jArray = obj.getJSONArray("rows");
        List<Jzny> jznyAllPage = new ArrayList<>();
        if (jArray.size() != 0) {
            for (int i = 0; i < jArray.size(); i++) {
                JSONObject jsonObject = jArray.getJSONObject(i);
                Jzny jz = new Jzny();
                jz.setMassifId(jsonObject.getString("id"));
                jz.setWindDirection(jsonObject.getString("fx"));
                jz.setWindSpeed(jsonObject.getString("fs"));
                jz.setRainfall(jsonObject.getString("yl"));
                jz.setAirTemperature(jsonObject.getString("kqwd"));
                jz.setAirHumidity(jsonObject.getString("kqsd"));
                jz.setSoilTemperature(jsonObject.getString("trwd"));
                jz.setSoilMoisture(jsonObject.getString("trsd"));
                jz.setLightIntensity(jsonObject.getString("gz"));
                jz.setcO2concentration(jsonObject.getString("eyht"));
                try {
                    jz.setCreatetime(sdf.parse(jsonObject.getString("createTime")));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String createTime1 = jsonObject.getString("createTime");
                jz.setTime(createTime1.substring(0, 4) + createTime1.substring(5, 7) + createTime1.substring(8, 10) + createTime1.substring(11, 13) + createTime1.substring(14, 16) + createTime1.substring(17, 19) + "00");

                jznyAllPage.add(jz);

            }
        }
        return JSON.toJSONString(jznyAllPage);
    }

    @RequestMapping("/jznypage")
    public Object jznypage(HttpServletRequest request) throws SQLException {
        String pagenum = request.getParameter("current");
        String landid = request.getParameter("landid");
        System.out.println("lanid------" + landid);

        Land land = landService.findLandName(landid);
        String userkey = land.getUserkey();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String url = "http://47.106.191.129/shuju/nydsjjk/listDo.html?userkey=" + userkey + "&pagenum=" + pagenum;

        String doPostString = ConnectionUtil.DoPostString(url, null);
        JSONObject obj = JSON.parseObject(doPostString);
        String titleNum = obj.getString("total");

        JSONArray jArray = obj.getJSONArray("rows");
        List<Jzny> jznyAllPage = new ArrayList<>();
        if (jArray.size() != 0) {
            for (int i = 0; i < jArray.size(); i++) {
                JSONObject jsonObject = jArray.getJSONObject(i);
                Jzny jz = new Jzny();
                jz.setMassifId(jsonObject.getString("id"));
                jz.setWindDirection(jsonObject.getString("fx"));
                jz.setWindSpeed(jsonObject.getString("fs"));
                jz.setRainfall(jsonObject.getString("yl"));
                jz.setAirTemperature(jsonObject.getString("kqwd"));
                jz.setAirHumidity(jsonObject.getString("kqsd"));
                jz.setSoilTemperature(jsonObject.getString("trwd"));
                jz.setSoilMoisture(jsonObject.getString("trsd"));
                jz.setLightIntensity(jsonObject.getString("gz"));
                jz.setcO2concentration(jsonObject.getString("eyht"));
                try {
                    jz.setCreatetime(sdf.parse(jsonObject.getString("createTime")));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String createTime1 = jsonObject.getString("createTime");
                jz.setTime(createTime1.substring(0, 4) + createTime1.substring(5, 7) + createTime1.substring(8, 10) + createTime1.substring(11, 13) + createTime1.substring(14, 16) + createTime1.substring(17, 19) + "00");

                jznyAllPage.add(jz);

            }
        }

        JSONObject jsonstr = new JSONObject();
        jsonstr.put("current", obj.getString("pagenum"));
        jsonstr.put("titleNum", titleNum);
        jsonstr.put("jznyAllPage", jznyAllPage);
        return JSON.toJSONString(jsonstr);
    }





}
